package cn.edu.zafu.bigdata.x.wupeng.g;

import java.io.*;

public class RhymingWords {
    public static void main(String[] args) throws IOException {
        FileReader words = new FileReader("files/words.txt");

        Reader in = reverse(sort(reverse(words)));
        //Reader in = reverse((words));
        BufferedReader bufferedReader = new BufferedReader(in);
        String input;
        try{
            while ((input = bufferedReader.readLine())!=null) {
                //input = bufferedReader.readLine();
                System.out.println(input);
                //System.out.println("ok");
            }
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
        in.close();
    }

    private static Reader reverse(Reader source) throws IOException {
        BufferedReader in =new BufferedReader(source);
        PipedWriter pipeOut = new PipedWriter();
        PipedReader pipeIn = new PipedReader(pipeOut);
        PrintWriter out =new PrintWriter(pipeOut);
        new ReverseThread(out, in).start();
        return pipeIn;
    }

    private static Reader sort(Reader source) throws IOException {
        BufferedReader in =new BufferedReader(source);
        PipedWriter pipeOut = new PipedWriter();
        PipedReader pipeIn = new PipedReader(pipeOut);
        PrintWriter out =new PrintWriter(pipeOut);
        new SortThread(out, in).start();
        return pipeIn;
    }
}
